"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _useFetch2 = _interopRequireDefault(require("../../useFetch"));
var logger = _interopRequireWildcard(require("../../logger"));
var useFetchInTable = function useFetchInTable(request) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var defaultParams = options.defaultParams,
    useFetchOptions = options.useFetchOptions,
    enableSuccessLog = options.enableSuccessLog,
    _options$enableFailLo = options.enableFailLog,
    enableFailLog = _options$enableFailLo === void 0 ? true : _options$enableFailLo,
    transfrom = options.transfrom,
    transfromParams = options.transfromParams;
  var _useState = (0, _react.useState)((0, _objectSpread2.default)({}, defaultParams)),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    params = _useState2[0],
    setParams = _useState2[1];
  var _useFetch = (0, _useFetch2.default)(function () {
      return request.apply(void 0, arguments).then(function (res) {
        if (res && res.success) {
          if (enableSuccessLog) logger.success('查询成功', res);
          return transfrom ? transfrom(res.data) : res.data;
        } else {
          if (enableFailLog) logger.fail('查询失败', res);
        }
      }).catch(function (err) {
        if (enableFailLog) logger.fail('查询失败', err);
      });
    }, useFetchOptions),
    fetch = _useFetch.fetch,
    data = _useFetch.data,
    loading = _useFetch.loading;
  var handleFetch = (0, _react.useCallback)(function (params) {
    setParams(params);
    var paramsTransformed = transfromParams ? transfromParams(params) : params;
    return fetch(paramsTransformed);
  }, []);
  /**
   * 使用原先的 pageSize 查询
   * @param pms
   */
  var search = (0, _react.useCallback)(function (pms) {
    return handleFetch((0, _objectSpread2.default)({
      // @ts-ignore
      current: 1,
      // @ts-ignore
      pageSize: params.pageSize
    }, pms));
  }, [params]);
  /**
   * 使用原先的 params 查询
   * @param pms
   */
  var research = (0, _react.useCallback)(function (pms) {
    return handleFetch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), pms));
  }, [params]);
  /**
   * 重置
   */
  var reset = (0, _react.useCallback)(function () {
    return handleFetch((0, _objectSpread2.default)({}, defaultParams));
  }, []);
  return {
    params: params,
    data: data,
    loading: loading,
    search: search,
    research: research,
    reset: reset
  };
};
var _default = useFetchInTable;
exports.default = _default;